/*
 * This file is part of "Deathwatch Ammo Counter for Android".
 *
 * "Deathwatch Ammo Counter for Android" is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * "Deathwatch Ammo Counter for Android" is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with "Deathwatch Ammo Counter for Android".  
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.TikiiTorch.Deathwatch.AmmoCounter.activities;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.TikiiTorch.Deathwatch.AmmoCounter.Ammunition;
import com.TikiiTorch.Deathwatch.AmmoCounter.R;
import com.TikiiTorch.Deathwatch.AmmoCounter.file.FileManager;

public class AmmoPicker extends Activity implements OnItemClickListener,
		OnItemLongClickListener {

	public static final int PICK_AMMO_REQUEST = 0x00;

	Ammunition[] matches;

	ListView list;

	String gunType;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.ammo_list);

		Bundle extra = getIntent().getExtras();

		if (extra != null) {
			gunType = extra.getString("gunType");
		}

		try {

			matches = FileManager.getAmmunitionList(this, gunType);

		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ListView ammoList = (ListView) findViewById(R.id.listview);
		ammoList.setOnItemClickListener(this);
		ammoList.setOnItemLongClickListener(this);
		ammoList.setAdapter(new ArrayAdapter<Ammunition>(this,
				R.layout.ammo_list_item, matches));

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Intent returnIntent = new Intent();

		returnIntent.putExtra("selectedAmmo", matches[position]);

		setResult(RESULT_OK, returnIntent);

		finish();
	}

	/**
	 * Called when the user long presses on an ammunition name in the list.
	 * 
	 * If the ammunition has a special effect, an alert dialog is displayed
	 * showing the special effect
	 */
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {

		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		String weapName = matches[position].getName();
		String weapEffect = matches[position].getEffect();

		if (weapEffect.equalsIgnoreCase("none")) {

			Toast.makeText(this, "No special effect for \"" + weapName + "\"",
					Toast.LENGTH_SHORT).show();

		} else {

			alert.setTitle(weapName);
			alert.setMessage(weapEffect);

			alert.setPositiveButton("Close", null);

			alert.show();

		}

		return true;
	}

}
